package views.job;

import org.intellij.lang.annotations.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Objects;
import java.util.Vector;

/**
 * ok
 * 添加职业的类
 */
public class JobAddFrame extends JFrame {
    int categoryid, employerid;
    ImageIcon imageIcon = new ImageIcon("src/images/job.png");
    private static final Logger LOGGER = LoggerFactory.getLogger(JobAddFrame.class);
    private JPanel panBtn = new JPanel();
    private JPanel panLab = new JPanel();
    private JLabel jL_job_name = new JLabel("职业名称：");
    private JLabel jL_job_category = new JLabel("职业类型：");
    private JLabel jL_employer_name = new JLabel("用人单位：");
    private JLabel jL_demand_num = new JLabel("需要人数：");
    private JLabel jL_hired_num = new JLabel("已招人数：");
    private JLabel jL_remark = new JLabel("备   注：");
    private JTextField jT_jobname = new JTextField();
    JComboBox<String> JobCategory = new JComboBox<String>();
    JComboBox<String> employName = new JComboBox<String>();
    private JTextField jT_demandNum = new JTextField();
    private JTextField jT_hiredNum = new JTextField();
    private JTextArea jTA_remark = new JTextArea(10, 10);
    private JButton btn_add = new JButton("添加");
    private JButton btn_reset = new JButton("重置");
    private JLabel jL_title = new JLabel();

    public JobAddFrame() {
        setTitle("添加职业");
        setSize(400, 400);
        setIconImage(imageIcon.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
        setResizable(true); // 不可改变窗口大小
        // 获取屏幕大小和当前frame的大小
        Dimension thisScreen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension thisFrame = this.getSize();
        // 使启动窗口位于屏幕的正中心
        setLocation((thisScreen.width - thisFrame.width) / 2,
                (thisScreen.height - thisFrame.height) / 2);
        // 设置单击窗口的【关闭】按钮时将发生相应的动作
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        jL_title.setFont(new Font("TsangerYuMo W03", Font.BOLD, 26));
        jL_title.setHorizontalAlignment(JLabel.CENTER);
        jL_title.setForeground(new Color(255, 51, 51));
        jL_title.setText("添  加  职  业");

        jL_job_name.setSize(100, 80);
        jL_job_name.setLocation(50, 5);
        jL_job_name.setHorizontalAlignment(JLabel.RIGHT);

        jL_job_category.setSize(100, 80);
        jL_job_category.setLocation(50, 40);
        jL_job_category.setHorizontalAlignment(JLabel.RIGHT);

        jL_employer_name.setSize(100, 80);
        jL_employer_name.setLocation(50, 80);
        jL_employer_name.setHorizontalAlignment(JLabel.RIGHT);

        jL_demand_num.setSize(100, 80);
        jL_demand_num.setLocation(50, 120);
        jL_demand_num.setHorizontalAlignment(JLabel.RIGHT);

        jL_hired_num.setSize(100, 80);
        jL_hired_num.setLocation(50, 160);
        jL_hired_num.setHorizontalAlignment(JLabel.RIGHT);

        jL_remark.setSize(100, 80);
        jL_remark.setLocation(50, 200);
        jL_remark.setHorizontalAlignment(JLabel.RIGHT);

        /*文本框位置*/
        jT_jobname.setSize(150, 25);
        jT_jobname.setLocation(160, 33);


        try {
            for (int k = 0; k < SelectCategoryname().size(); k++) {
                JobCategory.addItem(SelectCategoryname().get(k));
            }
            JobCategory.setSize(150, 25);
            JobCategory.setLocation(160, 70);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            for (int k = 0; k < SelectEmployerName().size(); k++) {
                employName.addItem(SelectEmployerName().get(k));
            }
            employName.setSize(150, 25);
            employName.setLocation(160, 110);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        jT_demandNum.setSize(150, 25);
        jT_demandNum.setLocation(160, 150);

        jT_hiredNum.setSize(150, 25);
        jT_hiredNum.setLocation(160, 190);

        jTA_remark.setSize(150, 60);
        jTA_remark.setLocation(160, 230);

        panBtn.add(btn_add);
        panBtn.add(btn_reset);
        panLab.setLayout(null);

        // panLab.add(jLabel1);
        panLab.add(jL_job_name);
        panLab.add(jL_job_category);
        panLab.add(jL_employer_name);
        panLab.add(jL_demand_num);
        panLab.add(jL_remark);
        panLab.add(jL_hired_num);

        panLab.add(jT_jobname);
        panLab.add(JobCategory);
        panLab.add(employName);
        panLab.add(jT_demandNum);
        panLab.add(jT_hiredNum);
        panLab.add(jTA_remark);

        add(jL_title, BorderLayout.NORTH);
        add(panBtn, BorderLayout.SOUTH);
        add(panLab, BorderLayout.CENTER);

        btn_add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                String Jobname = jT_jobname.getText().trim();
                String categoryname = Objects.requireNonNull(JobCategory.getSelectedItem()).toString().trim();
                try {
                    categoryid = seleCategoryid(categoryname);
                } catch (SQLException e) {
                    LOGGER.info("错误信息：" + e.getMessage());
                }

                String employername = Objects.requireNonNull(employName.getSelectedItem()).toString().trim();
                try {
                    employerid = seleEmlpoyerid(employername);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                int demNum = Integer.parseInt(jT_demandNum.getText());
                int hirNum = Integer.parseInt(jT_hiredNum.getText());
                String Remark = jTA_remark.getText().trim();

                Statement stmt = DBUtil.getStatement();
                @Language("MySQL")
                String sql2 = "insert into job_info(job_name,category_id,employer_id,demand_num,hired_num,remark) value("
                        + "'"
                        + Jobname
                        + "',"
                        + categoryid
                        + ","
                        + employerid
                        + ","
                        + demNum
                        + ","
                        + hirNum
                        + ",'"
                        + Remark
                        + "')";
                try {
                    stmt.executeUpdate(sql2);
                    JOptionPane.showMessageDialog(null, "添加成功。");
                    dispose();
                } catch (Exception e) {
                    LOGGER.info("错误信息：" + e.getMessage());
                }
            }
        });

        btn_reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                jT_demandNum.setText("");
                jT_hiredNum.setText("");
                jTA_remark.setText("");
            }
        });
    }

    public Vector<String> SelectCategoryname() throws SQLException {
        Vector<String> Category_name = new Vector<String>();
        String sql = "SELECT category_name from job_category ORDER BY category_id";
        Statement stmt = DBUtil.getStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            Category_name.add(rs.getString("category_name"));
        }
        DBUtil.closeMySQL();// 关闭连接
        return Category_name;
    }

    public Vector<String> SelectEmployerName() throws SQLException {
        Vector<String> Employer_name = new Vector<String>();
        String sql = "SELECT employer_name from employer ORDER BY employer_id";
        Statement stmt = DBUtil.getStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            Employer_name.add(rs.getString("employer_name"));
        }
        DBUtil.closeMySQL();// 关闭连接
        return Employer_name;
    }

    public int seleCategoryid(String name) throws SQLException {
        @Language("MySQL")
        String sql = "SELECT category_id from job_category WHERE category_name='" + name + "';";
        int count = 0;
        Statement pr = DBUtil.getStatement();
        ResultSet rs = pr.executeQuery(sql);
        while (rs.next()) { // 遍历数据库的数据
            count = rs.getInt(1);
        }
        DBUtil.closeMySQL();// 关闭连接
        return count;
    }

    public int seleEmlpoyerid(String employerName) throws SQLException {
        @Language("MySQL")
        String sql = "SELECT employer_id from employer WHERE employer_name='" + employerName + "';";
        int count = 0;
        Statement pr = DBUtil.getStatement();
        ResultSet rs = pr.executeQuery(sql);
        while (rs.next()) { // 遍历数据库的数据
            count = rs.getInt(1);
        }
        DBUtil.closeMySQL();// 关闭连接
        return count;
    }

/*防止sql注入的新写法
 public int seleEmlpoyerid(String employerName) throws SQLException {
        Connection conn = DBUtil.getConn();
        @Language("MySQL")
        String sql = "SELECT employer_id from employer WHERE employer_name=?;";
        int count = 0;
        PreparedStatement pr = conn.prepareStatement(sql);
        pr.setString(1, employerName);
        ResultSet rs = pr.executeQuery();
        while (rs.next()) { // 遍历数据库的数据
            count = rs.getInt(1);
        }
        DBUtil.closeMySQL();// 关闭连接
        return count;
    }*/
}
